<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-button icon="el-icon-plus" type="primary" @click="addRoleVisible=true">添加角色</el-button>

            <el-table
              border
              :data="roleData"
              style="width: 100%; margin: 15px 0"
              :default-sort="{ prop: 'date', order: 'descending' }"
            >
              <el-table-column type="index" label="序号" width="180" />
              <el-table-column
                prop="name"
                label="角色名"
                sortable
                width="180"
              />
              <el-table-column prop="description" label="描述" sortable />
              <el-table-column label="操作" sortable>
                <template slot-scope="{row}">
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="onEdit(row)">修改</el-button>
                  <el-button type="text">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination
              :current-page="queryData.page"
              :page-sizes="[2, 5, 10, 20]"
              :page-size="queryData.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-row>
              <el-col :offset="3" :span="8">
                <el-form ref="form" label-width="80px">
                  <el-form-item label="公司名称">
                    <el-input v-model="company.name" disabled />
                  </el-form-item>
                  <el-form-item label="公司地址">
                    <el-input v-model="company.companyAddress" disabled />
                  </el-form-item>
                  <el-form-item label="公司电话">
                    <el-input v-model="company.companyPhone" disabled />
                  </el-form-item>
                  <el-form-item label="邮编">
                    <el-input v-model="company.mailbox" disabled />
                  </el-form-item>
                  <el-form-item label="备注">
                    <el-input v-model="company.remarks" disabled />
                  </el-form-item>
                </el-form>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>

    <!-- 添加角色弹层 -->
    <el-dialog
      title="提示"
      :visible.sync="addRoleVisible"
      width="50%"
      @close="addDialogClose"
    >
      <el-form ref="addForm" :model="addRoleForm" :rules="addRoleRules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addRoleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述	" prop="description">
          <el-input v-model="addRoleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRoleVisible = false">取 消</el-button>
        <el-button type="primary" @click="onAddSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCompanyInfo } from '@/api/company'
import { getRoleList, addRoleList, editRoleList } from '@/api/role'
import { mapGetters } from 'vuex'
export default {
  nmae: 'Setting',
  data() {
    return {
      activeName: 'first',
      company: {},
      roleData: [],
      queryData: {
        page: 1,
        pagesize: 2
      },
      total: 10,
      addRoleVisible: false,
      addRoleForm: {
        name: '',
        description: ''
      },
      addRoleRules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        description: [{ required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }]
      }
    }
  },

  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.onloadCompany()
    this.onLoadRole()
  },

  methods: {
    // 加载公司信息
    async onloadCompany() {
      const res = await getCompanyInfo(this.companyId)
      this.company = res
    },

    // 发送请求
    async onLoadRole() {
      const res = await getRoleList(this.queryData)
      this.roleData = res.rows
      this.total = res.total
    },
    // 点击每一页数据跳转
    handleCurrentChange(val) {
      this.queryData.page = val
      this.onLoadRole()
    },
    handleSizeChange(val) {
      this.queryData.pagesize = val
      this.onLoadRole()
    },

    // 点击弹框确定校验 发请求
    async  onAddSave() {
      try {
        await this.$refs.addForm.validate()
        if (this.addRoleForm.id) {
          await editRoleList(this.addRoleForm)
        } else {
          addRoleList(this.addRoleForm)
          this.$message.success('添加成功')
        }

        this.addRoleVisible = false
        this.onLoadRole()
      } catch (err) {
        console.log(err)
      }
    },

    // 点击修改
    onEdit(row) {
      this.addRoleVisible = true
      this.addRoleForm = { ...row }
    },
    // 关闭对话框
    addDialogClose() {
      this.$refs.addForm.resetFields()
      this.addRoleForm = {
        name: '',
        description: ''
      }
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-tabs__header {
  .el-tabs__item {
    width: 140px;
    text-align: center;
  }
}
</style>
